/**
 * 删除节点、边
 * @param { HTMLElement }  elementGraph -- antv x6 元素
 */
export const getNodeOrEdgeDel = (elementGraph: any) => {
  let delSelected: boolean = false; // 是否已弹确认框
  elementGraph.bindKey("delete", () => {
    const getSelectedDel = elementGraph.getSelectedCells();
    if (getSelectedDel.length === 0 || delSelected) return;
    delSelected = true;
    elementGraph.on("node:unselected", () => {
      delSelected = false;
    });
    // 判断框选是否全部是节点（n）
    const getNodeShape = getSelectedDel.every((item: { shape: string }) => item.shape === "node-point");

    // 框选删除节点
    const filteredData = getSelectedDel.filter((item: { shape: string }) => item.shape === "node-point");
    // 框选删除功能区
    const filteredAreaData = getSelectedDel.filter((item: { shape: string }) => item.shape === "node-area");

    if (filteredData.length || filteredAreaData.length) {
      // 提示
      let deleteDataFilter: any;
      const deleteData = filteredData.map((item: { id: string }) => item.id);
      if (deleteData.length > 10) {
        deleteDataFilter = deleteData.slice(0, 10).join(",");
      } else {
        deleteDataFilter = deleteData;
      }
      const message = getNodeShape
        ? `是否删除【 ${deleteDataFilter} 】${deleteData.length > 10 ? "等" : ""}节点，且相关联的边也会跟着删除。删除后不可以恢复?`
        : "是否删除选中的节点，删除后不可以恢复?";
      ElMessageBox.confirm(`${message}`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })

        .then(() => {
          // 批量删除节点
          elementGraph.removeCells(filteredData);
          filteredAreaData.length && deleteArea(filteredAreaData);
        })
        .finally(() => {
          delSelected = false;
        });
    }
    elementGraph.on("edge:unselected", () => {
      delSelected = false;
    });
    // 框选删除边
    const filteredEdgeData = getSelectedDel.filter((item: { shape: string }) => item.shape === "edge");
    if (filteredEdgeData.length > 0) {
      // 提示
      let deleteDataEdgeFilter: any;
      const deleteEdgeData = filteredEdgeData.map((item: { id: string }) => item.id);
      if (deleteEdgeData.length > 10) {
        deleteDataEdgeFilter = deleteEdgeData.slice(0, 10).join(",");
      } else {
        deleteDataEdgeFilter = deleteEdgeData;
      }
      const message = `是否删除【 ${deleteDataEdgeFilter} 】${deleteEdgeData.length > 10 ? "等" : ""}边，删除后不可以恢复?`;
      ElMessageBox.confirm(`${message}`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 批量删除节点
          elementGraph.removeCells(filteredEdgeData);
        })
        .finally(() => {
          delSelected = false;
        });
    }
    // 框选删除功能区节点
    const deleteArea = (filteredAreaData: any[]) => {
      elementGraph.removeCells(filteredAreaData);
    };
  });
};
